import 'package:flutter/material.dart';

/// @date 2020/07/21  16:16
/// @author 程志强
/// @des grid view

class BuilderGridViewRoute extends StatefulWidget {
  @override
  _BuilderGridViewRouteState createState() => _BuilderGridViewRouteState();
}

class _BuilderGridViewRouteState extends State<BuilderGridViewRoute> {
  List<IconData> _icons = []; //保存Icon数据

  @override
  void initState() {
    super.initState();
    _retrieveIcons();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("GridView.builder"),
      ),
      body: GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3, //每行三列
            childAspectRatio: 1.0 //显示区域宽高相等
            ),
        itemCount: _icons.length,
        itemBuilder: _itemBuilder,
      ),
    );
  }

  void _retrieveIcons() {
    Future.delayed(Duration(milliseconds: 200)).then((value) => setState(() => {
          _icons.addAll([
            Icons.ac_unit,
            Icons.airport_shuttle,
            Icons.all_inclusive,
            Icons.beach_access,
            Icons.cake,
            Icons.free_breakfast
          ])
        }));
  }

  Widget _itemBuilder(BuildContext context, int index) {
    //如果显示到最后一个并且Icon总数小于200时继续获取数据
    if (index == _icons.length - 1 && _icons.length < 200) {
      _retrieveIcons();
    }
    return Icon(_icons[index]);
  }
}
